package com.strawcom.charmring.ui;

import org.kymjs.kjframe.utils.DensityUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;

import com.igexin.sdk.PushManager;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.util.AnimUtil;
import com.strawcom.charmring.util.PreferenceUtil;

/**
 * 
 * @ClassName: FirstActivity
 * @Description:首次运行可见
 * @author liyongqiang
 * @date 2015-3-30 下午11:47:12
 * 
 */
public class FirstActivity extends BaseActivity implements OnClickListener {

	private Button btnBuy;
	private Button btnLogin;
	private Button btnRegister;
	private int width, height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_first);
		super.onCreate(savedInstanceState);
		PushManager.getInstance().initialize(this.getApplicationContext());
		initViews();
		
	}

	private void initViews() {
		btnBuy = (Button) findViewById(R.id.btn_buy);
		btnLogin = (Button) findViewById(R.id.btn_login);
		btnRegister = (Button) findViewById(R.id.btn_register);

		btnBuy.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		btnRegister.setOnClickListener(this);
		width = DensityUtils.getScreenW(this);
		height = DensityUtils.getScreenH(this);
		if (PreferenceUtil.readBoolean(this, C.sp.isFirst, true)) {
			Animation buyAnimation = AnimUtil
					.init()
					.addTranslate(-width, btnBuy.getX(), -height,
							btnBuy.getY(), 300, 300,
							new OvershootInterpolator()).create();

			Animation loginAnimation = AnimUtil
					.init()
					.addTranslate(width, btnBuy.getX(), height, btnBuy.getY(),
							600, 300, new OvershootInterpolator()).create();
			Animation registerAnimation = AnimUtil
					.init()
					.addTranslate(width / 2, btnBuy.getX(), height,
							btnBuy.getY(), 900, 500,
							new OvershootInterpolator()).create();
			btnBuy.startAnimation(buyAnimation);
			btnRegister.startAnimation(registerAnimation);
			btnLogin.startAnimation(loginAnimation);
			PreferenceUtil.write(this, C.sp.isFirst, false);
		}else{
			String phone=PreferenceUtil.readString(this, C.sp.phone,"");
			String country = PreferenceUtil.readString(this, C.sp.phone_ex, "");
			String password=PreferenceUtil.readString(this, C.sp.password,"");

			if (country.equals("")) {
				phone = phone.substring(country.length());
			}
			if(!"".equals(phone)&&!"".equals(password)){
				if(PreferenceUtil.readBoolean(this, C.sp.from_push)){
					actionStart(MainActivity.class,C.extras_fromPush,true);
				}else{
					actionStart(MainActivity.class,C.extras_fromPush,false);
				}

				finish();
			}
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_buy:
			openBrowser(C.app.download_url);
			break;
		case R.id.btn_login:
			startActivity(new Intent(this, LoginActivity.class));
			break;
		case R.id.btn_register:
			startActivity(new Intent(this, RegisterActivity.class));
			break;
		default:
			break;
		}
	}
}
